#pragma once
#include <algorithm>

class Vector {
public:
    // Vector constructor - allocates memory.
    explicit Vector(int length)
    : _length(length), _data(new double[length]) {}
    // Vector destructor - frees memory.
    ~Vector() { delete _data; }

    // Indexing operators.  
    // Returns a reference and element, this can change the vector.
    double& operator()(int i) { return _data[i]; }
    // This returns a copy of an element, cannot change the vector.
    double  operator()(int i) const { return _data[i]; }

    // This returns the number of elements in the vector.
    int size() const { return _length; }

    // Swaps the contents of a vector with this one.
    void swap(Vector &x);
        
private:
    double *_data;
    int _length;    
};


// This function overwrites the operator << for vectors.
// It will allow you to use cout for vectors.
template<class stream>
stream& operator<<(stream &o, const Vector &x) {
    // If the vector is empty.
    if (x.size() == 0) {
        o << "()";
        return o;
    }
    
    o << "(" << x(0);
    for (int i=1; i<x.size(); ++i)
        o << ", " << x(i);
    o << ")\n";
    return o;
}

inline void Vector::swap(Vector &x) {
    std::swap(x._length, _length);
    std::swap(x._data,   _data);
}
